package baiduDemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baidu.aip.face.AipFace;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/10/10.
 */
public class BaiduDemo {

    public static final String APP_ID = "10223381";
    public static final String API_KEY = "B3ORkbTLLWGWq4dhuTF9VG11";
    public static final String SECRET_KEY = "YthmHWz4u93FdNyoDqA3bCOSeSdiqfzE";
    private AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

    public void registPerson(String picPath, String personId, List<String> group) {
        HashMap<String, String> options = new HashMap<String, String>();
        JSONObject res = client.addUser(personId, "test_user_info", group, picPath, options);
        System.out.println(res.toString(2));
    }

    public void verify (String picPath, String personId, List<String> group) {
        HashMap<String, Object> options = new HashMap<String, Object>(1);
        options.put("top_num", 5);
        JSONObject res = client.verifyUser(personId, group, picPath, options);
        System.out.println(res.toString(2));
    }

    public BaiduSearchDto identify(String picPath, List<String> group) {
        BaiduSearchDto baiduSearchDto = new BaiduSearchDto();
        HashMap<String, Object> options = new HashMap<String, Object>(1);
        options.put("user_top_num", 5);
        options.put("ext_fields", "faceliveness");
        JSONObject res = client.identifyUser(group, picPath, options);
        if (res.has("error_msg")) {
            baiduSearchDto.setResult("Error");
        } else {
            baiduSearchDto.setResult("OK");
            BaiduSearchResultDto baiduUidResultDto = JSON.parseObject(String.valueOf(res), new TypeReference<BaiduSearchResultDto>() {});
            baiduSearchDto.setConfindece(baiduUidResultDto.getResult().get(0).getScores());
            baiduSearchDto.setResult(baiduUidResultDto.getResult().get(0).getUid());
        }
        return baiduSearchDto;
//        System.out.println(res.toString(2));
    }

    public Integer detect(String picPath) {
        HashMap<String, String> options1 = new HashMap<String, String>();
        options1.put("max_face_num", "20");
        options1.put("face_fields", "qualities");
        JSONObject response = client.detect(picPath, options1);
//        System.out.println(response.toString());
        if (response.has("error_code")) {
            return -1;
        } else {
            return (Integer) response.get("result_num");
        }
    }


}
